package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.ExamineQuestionSubject;
import edu.jptest.util.GetDataBaseConnection;

public class ExamineQuestionSubjectDAO {

	// 插入卷题目
	public boolean insertExamineQuestionSubject(
			ExamineQuestionSubject examineQuestionSubject) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into e_q_subject(examine_paper_id,question_subject_id,num,self_subject) values(?,?,?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionSubject.getExaminePaperId());
			pstm.setInt(2, examineQuestionSubject.getQuestionSubjectId());
			pstm.setInt(3, examineQuestionSubject.getNum());
			pstm.setString(4, examineQuestionSubject.getSelfSubject());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除卷题目
	public boolean deleteExamineQuestionSubject(int examineQuestionSubject_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from e_q_subject where e_q_subject_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionSubject_id);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该卷题目
	public boolean updateExamineQuestionSubject(
			ExamineQuestionSubject examineQuestionSubject) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update e_q_subject set examine_paper_id=?,question_subject_id=?,num=?,self_subject=? where e_q_subject_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionSubject.getExaminePaperId());
			pstm.setInt(2, examineQuestionSubject.getQuestionSubjectId());
			pstm.setInt(3, examineQuestionSubject.getNum());
			pstm.setString(4, examineQuestionSubject.getSelfSubject());
			pstm
					.setInt(5, examineQuestionSubject
							.getExamineQuestionSubjectId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过卷题目id，查询卷题目对象
	public ExamineQuestionSubject getExamineQuestionSubjectByExamineQuestionSubjectId(
			int examineQuestionSubject_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		ExamineQuestionSubject examineQuestionSubject = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select * from e_q_subject where e_q_subject_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionSubject_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestionSubject.setExamineQuestionSubjectId(resultSet
						.getInt(1));
				examineQuestionSubject.setExaminePaperId(resultSet.getInt(2));
				examineQuestionSubject
						.setQuestionSubjectId(resultSet.getInt(3));
				examineQuestionSubject.setNum(resultSet.getInt(4));
				examineQuestionSubject.setSelfSubject(resultSet.getString(5));
				return examineQuestionSubject;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过试卷id，查询该试卷中所有的卷题目id（内包含题目信息及其它关于题目的信息）
	public List<Integer> getExamineQuestionSubjectIdByExaminePaperId(
			int examinePaper_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examineSubjectsId = new ArrayList<Integer>();
		try {
			String sql = "select e_q_subject_id from e_q_subject where examine_paper_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examinePaper_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineSubjectsId.add(resultSet.getInt(1));
			}
			return examineSubjectsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过题目在试卷中的位置，查询该试卷中该位置的卷题目id（内包含题目信息及其它关于题目的信息）
	public int getExamineQuestionSubjectIdByNumAndE_P_Id(int examinePaper_id,
			int examinePaper_num) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select e_q_subject_id from e_q_subject where examine_paper_id=? and num=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examinePaper_id);
			pstm.setInt(2, examinePaper_num);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// 获得所有卷题目id
	public List<Integer> getAllExamineQuestionSubjectsId() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examineQuestionSubjectsId = new ArrayList<Integer>();
		try {
			String sql = "select e_q_subject_id from e_q_subject";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestionSubjectsId.add(resultSet.getInt(1));
			}
			return examineQuestionSubjectsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}
}
